﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Com.Rivyer.Web.work
{
    public partial class daydoneplan : System.Web.UI.Page
    {
        private readonly BLL.userinfoBll infobll = new BLL.userinfoBll();
        private readonly BLL.jobdoneplanBll jobbll = new BLL.jobdoneplanBll();
        private readonly BLL.userBll userbll = new BLL.userBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            //非法操作验证
            if (Session["username"] == null
                || Session["username"].ToString().Trim().Length == 0)
            {
                Response.Write("<script>top.location='./login.aspx'</script>");
            }

            //如果表中有记录读出
            if (!IsPostBack
                && jobbll.GetList(1, string.Format("createdate = '{0}' and realname='{1}'", Convert.ToString(DateTime.Now.Date.ToShortDateString()), infobll.GetModel(Session["username"].ToString().Trim()).realname), "id desc").Tables[0].Rows.Count == 1)
            {
                Model.jobdoneplanModel jobmodel = jobbll.GetModelList(string.Format("createdate = '{0}' and realname='{1}'", Convert.ToString(DateTime.Now.Date.ToShortDateString()), infobll.GetModel(Session["username"].ToString().Trim()).realname))[0];
                createdate.Text = Convert.ToString(jobmodel.createdate);
                realname.Text = jobmodel.realname;
                daydone.Text = jobmodel.daydone;
                dayplan.Text = jobmodel.dayplan;
                needfixissue.Text = jobmodel.dayissue;
            }
            else
            {
                createdate.Text = Convert.ToString(DateTime.Now.Date.ToShortDateString());
                realname.Text = infobll.GetModel(Session["username"].ToString().Trim()).realname;
            }
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            //收集数据
            Model.jobdoneplanModel jobmodel = new Model.jobdoneplanModel();
            jobmodel.createdate = createdate.Text;
            jobmodel.realname = infobll.GetModel(Session["username"].ToString().Trim()).realname;
            jobmodel.daydone = daydone.Text.Trim();
            jobmodel.dayplan = dayplan.Text.Trim();
            if (needfixissue.Text.Trim().Length == 0)
            {
                jobmodel.dayissue = " ";
            }
            else
            {
                jobmodel.dayissue = needfixissue.Text.Trim();
            }

            //表中存在这条数据,就更新自己一条数据,不然插入   (插入是插入系统中所有的user的空白记录!方便休班的同志由晓红手动修改)
            if (jobbll.GetList(1, string.Format("createdate = '{0}' and realname='{1}'", Convert.ToString(DateTime.Now.Date.ToShortDateString()), infobll.GetModel(Session["username"].ToString().Trim()).realname), "id desc").Tables[0].Rows.Count == 1)
            {
                Model.jobdoneplanModel job1model = jobbll.GetModelList(string.Format("createdate = '{0}' and realname='{1}'", Convert.ToString(DateTime.Now.Date.ToShortDateString()), infobll.GetModel(Session["username"].ToString().Trim()).realname))[0];
                jobmodel.id = job1model.id;
                if (jobbll.Update(jobmodel))
                {
                    Model.useroperatelogModel logmodel = new Model.useroperatelogModel();
                    new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "更新", string.Format("每日工作完成与计划;日期:{0}", jobmodel.createdate));
                    Maticsoft.Common.MessageBox.Show(this, "更新成功!");
                }
            }
            else
            {
                //插入所有人的空白完成计划记录数
                List<Model.userinfoModel> infomodels = new BLL.userinfoBll().GetModelList("");
                Model.jobdoneplanModel tempjobdonemodel = new Model.jobdoneplanModel();
                foreach (Model.userinfoModel infomodel in infomodels)
                {
                    if (new BLL.userauthorityBll().GetModel(infomodel.username.Trim()).isminister == 1)
                    {
                        //部长.跳过
                    }
                    else
                    {
                        if (infomodel.realname == jobmodel.realname)
                        {
                            jobbll.Add(jobmodel);
                        }
                        else
                        {
                            tempjobdonemodel.realname = infomodel.realname;
                            tempjobdonemodel.createdate = DateTime.Now.ToShortDateString();
                            tempjobdonemodel.dayissue = "";
                            tempjobdonemodel.daydone = "";
                            tempjobdonemodel.dayplan = "";
                            jobbll.Add(tempjobdonemodel);
                        }
                    }
                }
                new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "第一个写入", "工作完成计划");
                Maticsoft.Common.MessageBox.Show(this,"添加成功!");
            }
        }
    }
}